package com.wzw.tcjh;

import java.util.*;

public class StrUtils {
    /**
     *  e.g.: turns a's home into a''s home so that it could be inserted into oracle.
     */
  public static String conv(String inString) {
		StringBuffer result = new StringBuffer();
		if (inString == null) {
			return "";
		}
		int i;
		for (i = 0; i < inString.length(); i++) {
			if (inString.charAt(i) == '\'') {
				result.append("\'").append(inString.charAt(i));
			} else {
				result.append(inString.charAt(i));
			}
		}
		return result.toString();
	}
  
  /**
   * format a line,let it can be in Oracle sql in block
   * @param str
   * @return
   */
  public static String formatLineIntoInBlock(String str){
	  if(str==null||"".equals(str)){
		  return "\'\'";
	  }
	  String[] retval = str.split(",");
	  StringBuffer sb = new StringBuffer();
	  for(int i=0;i<retval.length;i++){
		  sb.append(",\'"+StrUtils.conv(retval[i])+"\'");
	  }
	  if(sb.length()>0)
		  return sb.substring(1);
	  
	  return "\'\'";
  }
  
  /**
   *  converse a string line into a map
   * @param ss a should be conversed string
   * @param format StringTokenizer spilt format
   * @return a parsed set of given string
   */
  public static Set formatStringsToSet(String ss,String format){
	  
	  StringTokenizer st = new StringTokenizer(ss,format);
	  Set<String> retval = new HashSet<String>();
	  while(st.hasMoreTokens()){
		  retval.add(st.nextToken());
	  }
	return retval;
	  
  }
  
  /**
   * converse a string line into a map, default split by ", ;"
   * @param ss a conversed string line
   * @return a parsed set of given string
   */
  public static Set formatStringsToSet(String ss){
	  
	return formatStringsToSet(ss,", ;");
	  
  }

  public static String convAddSlash(String inString) {
    String result = "";
    int i;
    for (i =0 ; i < inString.length() ; i++) {
      if (inString.charAt(i) == '\'') {
        result = result + "\\" + inString.charAt(i);
      } else {
        result = result + inString.charAt(i);
      }
    }
    return result;
  }


  //Steven add from here
  public static String removeChar(String inputstring,char removechar)
	{
		if(inputstring==null)
		{
			return "";
		}
		String ret = "";
		for(int i=0;i<inputstring.length();i++)
		{
			if(inputstring.charAt(i)!=removechar)
			{
				ret = ret + inputstring.charAt(i);
			}
		}
		return ret;
    }
  //end
  
  //Ryan add from here
  public static String addFrontZero(String inputString){
	  String result;
	  result=(Float.parseFloat(inputString)<1&&Float.parseFloat(inputString)>0)?("0"+inputString):inputString;
	  return  result;
  }
  //end
  
  public static String nullToEmpty(String inputstring)
       {
               return (inputstring == null||inputstring.equals("null") ? "" : inputstring);
   }

  //generate a unique string for tmp filename
  public static String tmpFilename() {
    Random r = new Random();
    return Calendar.getInstance().getTimeInMillis() + "-" + Math.abs(r.nextInt());
  }

  public static String tmpFilename(String suf) {
    return tmpFilename() + suf;
  }

  public boolean digitCompare(String str_a,int b){
      double num_a = 0;
      int num_b = 1;
      try{
          num_a = Double.parseDouble(str_a);
      }catch (NumberFormatException ne){
          return false;
      }

      for (int i = 0;i < b;i++){
          num_b = num_b*10;
      }

      if (Math.abs(num_a) < num_b){
          return true;
      }
      else{
          return false;
      }
  }

  // return true, if target string is empty , or false
  public static boolean isEmptyString(String target) {
      boolean emptyString = true;
      for (int i = 0; i < target.length(); i ++) {
          if (target.charAt(i) != ' ') {
              emptyString = false;
              break;
          }
      }
      return emptyString;
  }
  
  public static List convertName(String fullname){
	  LinkedList returnName = new LinkedList();
	  String firstname = "";
	  String lastname = "";
	  fullname = fullname.trim();
	  int charIndex = fullname.indexOf(',');
	  
	  if (charIndex != -1) {
		  lastname = fullname.substring(0,charIndex).trim();
		  firstname = fullname.substring(charIndex+1).trim();
	  } else {
		  charIndex = fullname.lastIndexOf(' ');
		  if (charIndex == -1) {
			  firstname = fullname;
			  lastname = "";
		  } else {
			  firstname = fullname.substring(0,charIndex).trim();
			  lastname = fullname.substring(charIndex).trim();
		  }
	  }
	  returnName.add(firstname);
	  returnName.add(lastname);
	  return returnName;
  }
  
  /**
   * 
   * @param phoneNum: 13981992899
   * @param from	3
   * @param length	7
   * @return 139****2899
   */
  public static String convertPhone(String phoneNum, int from, int length){
	  return phoneNum.replace(phoneNum.subSequence(from, length), "******".subSequence(2, 6));
  }
      

  public static void main(String[] argv){
     /* List name = StrUtils.convertName("     Lawrence   ");
      System.out.println("" + name);*//*
	  System.out.println(StrUtils.formatLineIntoInBlock("s e ,fe,,fe"));
	  System.out.println(StrUtils.formatLineIntoInBlock(""));
	  System.out.println(StrUtils.formatLineIntoInBlock(null));
	  System.out.println(StrUtils.formatLineIntoInBlock(null));*/
	  System.out.println(StrUtils.formatStringsToSet("1sfe,2sfef,3sss 4sfe;5dfe;6sfef"));
	  System.out.println(convertPhone("13981992899", 3, 7));
  }
}
